//
//  ViewController.m
//  通讯录
//
//  Created by dllo on 16/6/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "Contact.h"
#import "AddressBook.h"
#import "ContactTableViewCell.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) AddressBook *addressBook;

@property (nonatomic, retain) UITableView *tableView;

@end

@implementation ViewController

-(void)dealloc
{
    [_tableView release];
    [_addressBook release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self config];
    
    [self handleData];

    [self createTableView];
}

-(void)config
{
    self.addressBook = [[AddressBook alloc]init];
    
    self.navigationItem.title = @"通讯录";
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
}

-(void)handleData
{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"Contact" ofType:@"plist"];
    
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
    
    NSArray *keysArray = [dictionary.allKeys sortedArrayUsingSelector:@selector(compare:)];
    
    for (NSString *key in keysArray) {
        
        NSArray *arrayOfContact = [dictionary objectForKey:key];
        
        NSMutableArray *contactArray = [NSMutableArray arrayWithCapacity:5];
        
        for (NSDictionary *dic in arrayOfContact) {
            
            Contact *contact = [[Contact alloc]init];
            
            [contact setValuesForKeysWithDictionary:dic];
            
            [contactArray addObject:contact];
        }
        [self.addressBook.dicOfContact setObject:contactArray forKey:key];
    }
//    NSLog(@"%@",self.addressBook.dicOfContact);
    
}

-(void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - 64) style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.backgroundColor = [UIColor colorWithRed:0.80 green:0.67 blue:0.92 alpha:1.0];
        
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
    self.tableView.rowHeight = 80;
    
    [self.tableView registerClass:[ContactTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self.tableView release];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    NSArray *keysArray = [self.addressBook getNameGroup];
    
    NSString *key = [keysArray objectAtIndex:indexPath.section];
    
    NSArray *array = [self.addressBook.dicOfContact objectForKey:key];
    
    Contact *contact = [array objectAtIndex:indexPath.row];
    
    cell.contact = contact;
        
    return cell;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.addressBook.dicOfContact.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *keysArray = [self.addressBook getNameGroup];
    
    NSString *key = [keysArray objectAtIndex:section];
    
    NSArray *array = [self.addressBook.dicOfContact objectForKey:key];
    
    return array.count;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSArray *keysArray = [self.addressBook getNameGroup];
    
    NSString *key = [keysArray objectAtIndex:section];
    
    return key;
}

- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSArray *keysArray = [self.addressBook getNameGroup];
    
    return keysArray;
}

@end
























